package com.sinoglobal.ningxia.qr;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.sinoglobal.ningxia.R;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * 
 * @author limeishu
 * @createdate 2014-9-4 下午4:58:58
 * @Description: 历史记录列表适配器
 */
public class HistoryListAdapter extends BaseAdapter {
	Activity activity;
	LayoutInflater mInflater;
	List<HistoryMessageVo> historyList = new ArrayList<HistoryMessageVo>();
	Calendar calendar = Calendar.getInstance();
	int dayNow = calendar.get(Calendar.DAY_OF_MONTH);
	int monthNow = calendar.get(Calendar.MONTH) + 1;
	int yearNow = calendar.get(Calendar.YEAR);

	public HistoryListAdapter(Activity activity) {
		this.activity = activity;
		mInflater = LayoutInflater.from(activity);
	}

	public List<HistoryMessageVo> getHistoryList() {
		return historyList;
	}

	public void setHistoryList(List<HistoryMessageVo> historyList) {
		this.historyList.addAll(historyList);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return historyList.size();
	}

	@Override
	public Object getItem(int position) {
		return historyList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		HistoryViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new HistoryViewHolder();
			convertView = mInflater
					.inflate(R.layout.qr_history_list_item, null);
			viewHolder.content = (TextView) convertView
					.findViewById(R.id.history_content);
			viewHolder.type = (TextView) convertView
					.findViewById(R.id.history_type);
			viewHolder.time = (TextView) convertView
					.findViewById(R.id.history_time);
			convertView.setTag(viewHolder);
		}
		viewHolder = (HistoryViewHolder) convertView.getTag();
		try {
			String type = historyList.get(position).getType();
			String history = historyList.get(position).getContent();
			int day = historyList.get(position).getDay();
			int month = historyList.get(position).getMonth();
			int year = historyList.get(position).getYear();
			String date = "";
			if ("1".equals(type)) {
				viewHolder.type.setText(activity.getString(R.string.qr_history_url));
				viewHolder.content.setText(history);
			} else if ("2".equals(type)) {
				viewHolder.type.setText(activity.getString(R.string.qr_history_text));
				viewHolder.content.setText(history);
			}

			if (year == yearNow) {
				if (month == monthNow) {
					if (0 == (dayNow - day)) {
						date = "今天";
					} else if (1 == (dayNow - day)) {
						date = "昨天";
					} else if (2 == (dayNow - day)) {
						date = "前天";
					} else {
						date = month + "月" + day + "日";
					}
				} else {
					date = month + "月" + day + "日";
				}

			} else {
				date = year + "年" + month + "月" + day + "日";
			}
			viewHolder.time.setText(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	class HistoryViewHolder {
		public TextView content;
		public TextView type;
		public TextView time;

	}
}
